<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>用户</title>
  <link rel="stylesheet" href="../../../layui/css/layui.css">
  <script src="../../../layui/layui.js"></script>
</head>
<style type="text/css">
  body{
    height: 100%;
    width: 100%;
    margin-top: 11px;
  }


</style>

<body >
<!-- 内容主体区域 -->
<div class="layui-fluid layui-form">
  <div class="layui-form-item">
    <div class="layui-input-inline " style=" width: 438px;">
      <button class="layui-btn layui-btn-primary addUser">新增用户</button>
      <button class="layui-btn updateStuff">修改用户</button>
      <button class="layui-btn layui-btn-normal deleteUser">删除用户</button>
      <!--      <button class="layui-btn">默认按钮</button>
            <button class="layui-btn layui-btn-normal">百搭按钮</button>-->
    </div>
    <div class="layui-inline">
      <label class="layui-form-label">搜索选择框</label>
      <div class="layui-input-inline">
        <select name="modules"  lay-search="" class="storeList"  lay-filter="storeid">
          <!-- <option value="">直接选择或搜索选择</option>
           <option value="user_code">用户账号</option>
           <option value="user_name">用户名称</option>
           <option value="email">email</option>-->
        </select>
      </div>

      <div class="layui-input-inline">
        <select name="modules"  lay-search="" class="stuff"  lay-filter="stuff">
           <option value="">直接选择或搜索选择</option>
           <option value="tel">电话号码</option>
           <option value="stuff_name">用户名称</option>
        </select>
      </div>
    </div>
    <div class="layui-inline">
      <div class="layui-input-inline">
        <input type="text" name="userKey" class="layui-input stuffKeyValue ">
      </div>
      <div class="layui-input-inline">
        <button class="layui-btn layuiadmin-btn-admin layui-btn-normal check" lay-submit="" lay-filter="LAY-user-back-search">
          <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
        </button></div>
    </div>
  </div>
</div>

<div id="pTable" >
  <table id="layui_table_id" class="layui-table" lay-skin="line" lay-filter="test" style="width: 1680px">

  </table>
  <!--  <script type="text/html" id="barDemo">
      <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
      <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>-->
  <div id="laypage"></div>
</div>
<input type="hidden" id="storeId" name="storeId " vlaue="" class="layui-input ">
</body>
</html>

<script type="text/javascript">
  layui.use(['form','laydate','layer','table','laypage','util'],function(){
    var form = layui.form;
    var laydate=layui.laydate;
    var $ = layui.$;
    productsearch(1);
    init();
    function init() {
      $.ajax({
        type:'get',
        url:'/hy/basic/timeSheet/getSalon',
        data:{page:1,limit:20},
        success: function(data){
          var dataList =data.data;
          var html="<option value=''>请选择门店</option>";
          for(var i=0;i<dataList.length;i++){
            html+="<option value='"+dataList[i].recordId+"'>"+dataList[i].salonName+"</option>";
          }
          $(".storeList").append(html);
          form.render('select');
        },
        error:function(data){
          alert("操作失败");
          console.log(data);
        }
      })
    }
    form.on('select(storeid)', function(data){
      $("#storeId").val(data.value);
    })
    function productsearch(page,filterExpr,storeId) {

      var table = layui.table,
              laydate = layui.laydate,
              laypage = layui.laypage;
      var form = layui.form;
      table.render({
        elem : '#layui_table_id',
        url : '/hy/basic/stuff/getStuffAdmin',
        cols : [ [
          {checkbox: true,
            filter:'test'
          }
          ,
          {
            field : 'recordId',
            title : 'recordId',
            sort : true,
            align : 'center'
          }
          , {
            field : 'stuffName',
            title : '员工名称',
            align : 'center'
          }
          , {
            field : 'tel',
            title : '电话号码',
            align : 'center',
          }
          , {
            field : 'qq',
            title : 'qq',
            align : 'center'
          }
          , {
            field : 'address',
            title : '地址',
            align : 'center'
          }

        ] ],
        page : true,
        where : {
          filterExpr:filterExpr,
          storeId:storeId
        },
        responseHandler: function (res, curr,count) {
          $('#pageNum').val(curr);
          console.log(res.listMap);
          var listMap = res.listMap;
          var chechId  = res.chechId;
          if(listMap.length>0){
            var recordId = listMap[0].recordId;
            $(".storeList").find("option[value = '"+recordId+"']").attr("selected","selected");
            form.render();//表单重新渲染，要不然添加完显示不出来新的option
            for(index in listMap){

             /* if(index==0){
                var temp = listMap[index].recordId;
                $("#storeId").val(temp);
              }
              var temp = "";
              if(chechId==listMap[index].recordId){
                temp = "<option selected='selected' value="+listMap[index].recordId+">"+listMap[index].salonName+"</option>";
              }else{
                temp = "<option value="+listMap[index].recordId+">"+listMap[index].salonName+"</option>";
              }

              $(".storeList").append(temp);
              form.render();//表单重新渲染，要不然添加完显示不出来新的option*/
            }
          }

          return {
            "count": res.total,
            "data": res.data,
            "code": 0,//code值为200表示成功
            "msg": ""
          }
        },
        done : function(res, curr, count) {
          console.log(res)
          $('#pageNum').val(curr);

        }
      })
      // table.on('checkbox(test)', function(obj){
      //   console.log(layui.table.checkStatus('layui_table_id'));
      // });
      // 监听工具条
      table.on('tool(test)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data, //获得当前行数据
                layEvent = obj.event; //获得 lay-event 对应的值
        if (layEvent == 'detail') {
          queryById(data.id);
        } else if (layEvent == 'del') {
          //alert(data.id)
          var pageNum = $('#pageNum').val();
          layer.confirm('真的删除行么', function(index){
            deleteById(data.id);
            layer.close(index);
            //向服务端发送删除指令
          });
        } else if (layEvent === 'edit') {
          layer.msg('编辑操作');
        }
      });
      //常规用法
      laydate.render({
        elem : '#createDate'
      });
      //常规用法
      laydate.render({
        elem : '#processingTime'
      });

    }

    $(".check").on("click", function() {

      var stuffKey=$('.stuff option:selected').val();
      var storeId = $("#storeId").val();
      var temp ="";
      if(stuffKey!=""){
        var stuffKeyValue = $('.stuffKeyValue').val();
        temp = stuffKey+"='"+stuffKeyValue+"'";
      }
      productsearch(1, temp,storeId);

    })





    $(".deleteUser").on("click",function(){
      var data = layui.table.checkStatus('layui_table_id').data;
      if(data.length<1){
        alert("请选中一条数据");
        return false;
      }
      var deleteList = [];
      for(var i=0 ;i<data.length;i++){
        var id = data[i].recordId;
        var temp =id+"";
        deleteList.push(temp);
      }
      console.log(deleteList);
      $.ajax({
        type:'POST',
        url:'/hy/basic/stuff/deleteStuff',
        data: JSON.stringify(deleteList),
        contentType: "application/json",
        success: function(data){
          if(data.msgcode="200"){
            alert("操作失败！");
          }else{
            alert("操作成功！");
            productsearch(1);
          }

        },
        error:function(data){
          alert("操作失败");
          console.log(data);
        }
      })

    })

    $(".addUser").on("click",function(){
      layer.open({
        type:2,
        area:[600+'px',600+'px'],
        fix:false,
        offset: '80px',
        maxmin:true,
        shadeClose:true,
        scrollbar: false,
        shade:0.4,
        title:'添加员工',
        content:['add_stuff.html'],
        success:function(layero,index){
          var iframe = window['layui-layer-iframe' + index];
          // 向子页面的全局函数child传参
          var stuff = {};
          stuff.storeId =  $("#storeId").val();

          iframe.child(stuff);
          console.log("成功");
        },
        fail:function(data){
          console.log(data);
        }
      })
    })
    $(".updateStuff").on("click",function(){
      console.log(layui.table.checkStatus('layui_table_id'));
      var data = layui.table.checkStatus('layui_table_id').data;
      if(data.length>1){
        alert("请选中一条数据");
        return false;
      }
      layer.open({
        type:2,
        area:[482+'px',315+'px'],
        fix:false,
        offset: '80px',
        maxmin:true,
        shadeClose:true,
        scrollbar: false,
        shade:0.4,
        title:'修改用户',
        content:['add_stuff.html'],
        success:function(layero,index){
          // 获取子页面的iframe
          var iframe = window['layui-layer-iframe' + index];
          // 向子页面的全局函数child传参
          var stuff = data[0];
          console.log(stuff);
          iframe.child(stuff);
          console.log("成功");
        },
        fail:function(data){
          console.log(data);
        }
      })
    })

  });
  function userStatus(t){
    if(t==0){
      return '启用';
    }else{
      return '禁用';
    }
    return util.toDateString(t1);
  }
</script>


<script type="text/html" id="userStatus">


  {{ userStatus(d.userStatus) }}
</script>